<?php


App::import('Controller', 'Tweets');
App::import('Model', 'Tweet');
App::import('Model', 'User');
App::import('Model', 'Post');
App::import('Component', 'Session');
App::import('Component', 'Auth');
App::import('Model', 'tattlrAccount');

class TweetsControllerTest extends ControllerTestCase {

    var $components = array('Auth', 'Cookie', 'Session', 'RequestHandler');

    function startTest() {
        $this->TweetsTest = new TweetsControllerTest();
        $this->TweetsTest->constructClasses();
        $this->TweetsTest->initialize($this->TweetsTest);
        $this->Tweets = $this->generate('Tweets', array(
            'components' => array('Session', 'Auth' => array('User'), 'RequestHandler') //We mock the Auth Component here
                ));
        $this->Tweet = ClassRegistry::init('Tweet');
        $this->Post = ClassRegistry::init('Post');
        $this->Account = ClassRegistry::init('Account');
    }

    function testfavorite() {
	//to run this test you must insert the three records user with userid 10 and account with id 517889290 and tattlr_accounts with id 517889290
        $this->Tweets->Auth->staticExpects($this->any())
                ->method('User')->with($this->logicalOr(
                                $this->equalTo('userID'), $this->equalTo('accID'), $this->equalTo('admin')))
                ->will($this->returnCallback(array($this, 'favoriteCallback')));
        $text = rand() . '' . rand();
        $arraydata = array('tweetText' => $text, 'accID' => $this->Tweets->Auth->User('accID'), 'userID' => $this->Tweets->Auth->User('userID'), 'sub_time' => '2012-05-16 12:34:19', 'eventID' => NULL, 'reply_to' => '202738752492285952');
        $status = ( $this->Account->connectionRequest($arraydata['accID'])->Post('statuses/update', array('status' => $arraydata['tweetText'], 'in_reply_to_status_id' => $arraydata['reply_to'])));
        $tweet = (array) $status;
        $this->Post->set(array('postID' => $tweet['id_str'], 'accID' => $arraydata['accID'], 'userID' => $arraydata['userID'], 'text' => $arraydata['tweetText'], 'time' => $arraydata['sub_time']));
        $this->Post->save();
        $this->Tweet->set(array('postID' => $tweet['id_str'], 'retweets' => 0, 'favorites' => 0, 'isMention' => 0, 'restored' => 0, 'restoreDate' => NULL, 'subTime' => $arraydata['sub_time'], 'restoredBY' => NULL, 'eventID' => $arraydata['eventID'], 'tweetInterval' => NULL, 'replyTo' => $arraydata['reply_to']));
        $this->Tweet->save();
        $tweet = $this->Post->query("select postID from posts where posts.text = " . "'" . $text . "'");
        $postid = $tweet[0]['posts']['postID'];
        $this->testAction('/tweets/favorite/' . $postid . '/0');
        $db = $this->Tweet->find('all', array('conditions' => array('postID' => $postid, 'favorited_by_us' => '1')));
        $this->assertEquals(count($db), 1);
    }

    function favoritecallback($foo) {
        if ($foo == 'accID')
            return 517889290;
        if ($foo == 'userID')
            return 10;
        return 0;
    }

    /**
     * Tests Unfavoriting a tweet
     *
     * @access: public
     *
     * @author "Hussein Abo El Seoud"
     */
    public function testUnfavorite() {
        $this->testAction('/tweets/unfavorite/203891764325662720/0', array('method' => 'get', 'return' => 'vars'));
        $db = $this->Tweet->find('all', array('conditions' => array('postID' => '203891764325662720', 'favorited_by_us' => '1')));
        $this->assertEquals(array(), $db);
    }

    public function testRetweet() {
        $this->Tweets->retweet('203373168788914176', '0');
        $result = $this->Tweet->find('all', array('conditions' => array('postID' => '203373168788914176', 'retweeted_by_us' => '0')));
        $exists = $this->query("SELECT twitteraccounts_retweet_tweets.postID, COUNT( * ) as count 
        FROM twitteraccounts_retweet_tweets
        WHERE twitteraccounts_retweet_tweets.postID ='202565681126965248'
        GROUP BY postID)");
        $this->assertTrue($exists == 0);
        $this->assertEquals(array(), $result);
    }
	public function testUndoRetweet() {

        $this->Tweets->undoRetweet('203373168788914176');
        $result = $this->Tweet->find('all', array('conditions' => array('postID' => '203891764325662720', 'retweeted_by_us' => '1')));
        $postID = $this->query("SELECT twitteraccounts_retweet_tweets.postID, COUNT( * ) as count 
        FROM twitteraccounts_retweet_tweets
        WHERE twitteraccounts_retweet_tweets.postID ='202565681126965248'
        GROUP BY postID)");
        $this->assertTrue($postID == 0);
        $this->assertEquals(array(), $result);
    }


    function endTest() {
        unset($this->Tweets);
        ClassRegistry::flush();
    }

}